import java.util.Scanner;
import org.junit.Test;
import java.util.Arrays;

public class NGuessNumber {
    public static int[] numbers = new int[4];
    public static int[] guesslist = new int[4];

    public static int numa = 0;
    public static int numb = 0;
    public static void main(String[] args) {
        System.out.println("欢迎来到新猜数字游戏!");

        while(true){
            System.out.println("[若您选择开始请输入1]");
            System.out.println("[若您选择结束请输入2]");
            Scanner playinput = new Scanner(System.in);

            switch(playinput.nextInt()){
                case 2:
                    System.out.println("您已退出游戏！");
                    System.exit(2);
                    break;
                case 1:
                    int cs = 0;
                    int sy = 7;

                    System.out.println("现在开始游戏(您有7次机会！)\n");

                    generateAnswer();

                    while (true) {
                        getPlayerInput();
                        compareGuessAnswer();
                        if (tNew() == true) {
                            System.out.println("您已获胜\n");
                            break;

                        } else {
                            cs++;
                            sy--;
                            if(cs==7){
                                System.out.println("7次到了！正确答案为"+Arrays.toString(numbers));
                                System.out.println("您是否再来一局？");
                                break;
                            }
                            System.out.println("猜错了，再来一次叭！(还剩"+sy+"次)\n");
                        }

                    }
                    break;
                default:
                    System.out.println("您输入的字符错误，请输入0/1\n");
            }
        }
    }

    public static int[] generateAnswer() {

        int nums = 0;

        while (true) {
            int random = (int) (Math.random() * 9 + 1);
            int i = 0;
            for (; i < numbers.length; i++) {
                if (random == numbers[i]) {
                    break;
                }
            }
            if (i == numbers.length) {
                numbers[nums++] = random;
            }
            if (nums == 4) {
                break;
            }

        }
        return numbers;
    }
    public static int[] getPlayerInput(){
        try{
            Scanner scanner = new Scanner(System.in);
            for(int in = 0;in<4;in++){
                System.out.println("请您输入0至9之间的 "+ (in+1) + "个数字为:");
                int input = scanner.nextInt();
                if(input>=10 || input<0){
                    System.out.printf("您输入的错误:");
                    in = in-1;
                }else{
                    guesslist[in] = input;
                }
            }
        }catch(Exception e){
            System.out.println("您输入的错误");
        }
        System.out.println(Arrays.toString(guesslist)+"\n");
        return guesslist;
    }
    public static String compareGuessAnswer(){
        numa = 0;
        numb = 0;
        for(int i = 0; i< numbers.length;i++){
            for(int j =0;j<numbers.length;j++){
                if(i!=1){
                    if(numbers[i] == guesslist[j]){
                        numb++;
                    }
                }else{
                    if(numbers[i] == guesslist[j]){
                        numa++;
                    }
                }
            }
        }
        System.out.println(numa+"A"+numb+"B");
        System.out.println("A:存在并且数字所在的位置正确个数,B:存在且数字所在位置不正确个数");
        System.out.println("如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置是对的，两个位置是不对的\n");

        return numa+"A"+numb+"B\n"+"A:存在且数字所在位置正确个数，B:存在且数字所在位置不正确个数！" +
                "\n"+"如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置是对的，两个位置是不对的";
    }
    public static boolean tNew(){
        boolean flag = false;
        if(numa==4){
            flag=true;
        }else {
            flag=false;
        }
        return flag;

    }
}








